import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Roster extends javax.swing.JFrame implements
ActionListener
{
	private JPanel jPanel1;
	private JTextField getDate;
	private JScrollPane jScrollPane1;
	private JTextArea jTextArea1;
	private JLabel jLabel1;
	private JPanel textExplain;
	private JButton getPersonalTimeTable;
	private int driverId;
	FileReader reader;


	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{

			public void run()
			{

				Roster inst = new Roster();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public Roster()
	{
		super();
		setTitle("Roster");
		initGUI();
	}
  
  private void initGUI() 
	{
		
		try
		{
			getContentPane().setLayout(null);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jPanel1 = new JPanel();
				BorderLayout jPanel1Layout = new BorderLayout();
				getContentPane().add(jPanel1);
				jPanel1.setLayout(jPanel1Layout);
				jPanel1.setBounds(0, 0, 380, 286);
				jPanel1.setOpaque(false);
				{
					getPersonalTimeTable = new JButton();
					jPanel1.add(getPersonalTimeTable, BorderLayout.SOUTH);
					getPersonalTimeTable.setText("Get it!");
					getPersonalTimeTable.setPreferredSize(new java.awt.Dimension(368, 30));
					getPersonalTimeTable.addActionListener(this);
				}
				{
					textExplain = new JPanel();
					jPanel1.add(textExplain, BorderLayout.CENTER);
					textExplain.setLayout(null);
					textExplain.setFont(new java.awt.Font("Andale Mono",0,10));
					{
						jLabel1 = new JLabel();
						textExplain.add(jLabel1);
						jLabel1.setText("Please type in start date(dd/mm/yyyy)");
						jLabel1.setBounds(7, 6, 294, 15);
					}
					{
						getDate = new JTextField();
						textExplain.add(getDate);
						getDate.setBounds(293, 3, 84, 22);
					}
				}
				{
					jScrollPane1 = new JScrollPane();
					jPanel1.add(jScrollPane1, BorderLayout.NORTH);
					jScrollPane1.setPreferredSize(new java.awt.Dimension(368, 230));
					{
						jTextArea1 = new JTextArea();
						jScrollPane1.setViewportView(jTextArea1);
						jTextArea1.setPreferredSize(new java.awt.Dimension(362, 2000));
					}
				}
			}
			pack();
			this.setSize(385, 308);
		} catch (Exception e)
		{
			// add your error handling code here
			e.printStackTrace();
		}
	}
  
	public void actionPerformed(ActionEvent event)
	{
		driverId = GUI.getDriverID();
		Date startDate = null;
		String startDateS = getDate.getText();
		boolean s = isValidDate(startDateS);
		if (s == true)
		{
			try
			{
				DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
				startDate = df.parse(startDateS);
			} catch (ParseException e)
			{
				System.err.println("Caught IOException: " + e.getMessage());
			}
		  controlerPanel.getPersonalTimeTable(driverId, startDate);
			//newCoutrolerPanel.getPersonalTimeTable(driverId, startDate);
      
		  try
			{
				reader = new FileReader("PersonalTimetable.txt");
				jTextArea1.read(reader, "PersonalTimetable.txt");
			} catch (IOException exception)
			{
				System.err.println("Load oops");
				exception.printStackTrace();
			} finally
			{
				if (reader != null)
				{
					try
					{
						reader.close();
					} catch (IOException exception)
					{
						System.err.println("Error closing reader");
						exception.printStackTrace();
					}
				}
			}
		}
		else
		{
			Object[] options = { "OK" };
			JOptionPane.showOptionDialog(null,
					"The date that you provided is invalid", "Warning",
					JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
					options, options[0]);
		}
	}
	
	public boolean isValidDate(String date)
	{
		// set date format dd/MM/yyyy
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

		// testDate variable
		Date testDate = null;

		// to parse the string into date form
		try
		{

			testDate = sdf.parse(date);

		}

		// if the format of the string provided doesn't match the format we
		// declared in SimpleDateFormat() we will get an exception

		catch (ParseException e)
		{

			return false;
		}

		// dateformat.parse will accept any date as long as it's in the format
		// you defined, it simply rolls dates over, for example, december 32
		// becomes jan 1 and december 0 becomes november 30
		// This statement will make sure that once the string
		// has been checked for proper formatting that the date is still the
		// date that was entered, if it's not, we assume that the date is invalid

		if (!sdf.format(testDate).equals(date))
		{
			return false;
		} else
			return true;

	} // end isValidDate
}
